<?php 
if(class_exists('tag') != true)
{
	class tag
	{
		/**
		 * get all possible tags and their ids from the database
		 * 
		 * returns an array $tags
		 * 		$tags = array( [ID] => [DESCRIPTION] )
		 * 
		 */
		static function getAllTags()
		{
			$DB = database::getDB();
			
			/*
			$sql = "select t.* from tags as t order by t.DESCRIPTION asc";
			$result = mysqli_query($DB, $sql);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
				$tags[$row['ID']] = $row['DESCRIPTION'];
				
			mysqli_free_result($result);
			mysqli_close($DB);
			*/
			
			$pq = $DB->prepare("SELECT tags.* FROM tags ORDER BY tags.description ASC");
			$pq->execute();
			$result = $pq->fetchAll();
			
			//echo '<pre>'.print_r($result, true).'</pre>';
			
			return $result;
		}
		
		/**
		 * get all possible tags/ids with the number of times they have each been tagged
		 * 
		 * returns an array $tags
		 * array(
		 * 		[ID] => array(
		 * 			[desc] => description	//name of the tag
		 * 			[count] => count		//number of times events have used this tag
		 * 		)
		 * )
		 */
		static function getAllTagsWithCount()
		{
			$DB = database::getDB();
			
			/*
			$sql = "select t.ID as ID, t.DESCRIPTION as DESCRIPTION, count(et.TAG_ID) as COUNTER from tags as t left join event_has_tag as et on (t.ID = et.TAG_ID) group by t.ID order by t.DESCRIPTION asc";
			$result = mysqli_query($DB, $sql);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
				$tags[$row['ID']] = array('desc' => $row['DESCRIPTION'], 'count' => $row['COUNTER']);
				
			mysqli_free_result($result);
			mysqli_close($DB);
			*/
			
			$sql = "SELECT t.id AS id, t.name AS name, COUNT(et.tag_id) AS count";
			$sql .= " FROM tags AS t LEFT JOIN event_has_tag AS et ON (t.id = et.tag_id) GROUP BY t.id";
			$pq = $DB->prepare($sql);
			$pq->execute();
			$result = $pq->fetchAll();
			
			return $result;
		}
		
		
		/**
		 *  display the tag cloud at the side of each page 
		 *  call this function when you want to display the tag cloud directly on the page
		 **/
		static function displayTagCloud()
		{ 
			$tags = tag::getAllTagsWithCount();
			//echo '<pre>'.print_r($tags, true).'</pre>';
			
			$tagCloud = tag::formatTagCloud($tags);
			echo '<div style="text-align:center;">'.$tagCloud.'</div>';
		}
		
		/**
		 * format the tag cloud
		 * helper function to ass classes to each tag
		 * returns string that needs to be displayed directly.
		 **/
		static function formatTagCloud($tags)
		{
			$string = '';
			$max = 6;
			$min = 0;
			$diff = $max-$min;
			$div = $diff/$max;
			
			foreach($tags as $ID => $tag)
			{
				if( ($tag['count'] >= $min) && ($tag['count'] < $min+$div*1) )
					$string .= '<form class="tag6"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				elseif( ($tag['count'] >= $min+$div*1) && ($tag['count'] < $min+$div*2) )
					$string .= '<form class="tag5"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				elseif( ($tag['count'] >= $min+$div*2) && ($tag['count'] < $min+$div*3) )
					$string .= '<form class="tag4"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				elseif( ($tag['count'] >= $min+$div*3) && ($tag['count'] < $min+$div*4) )
					$string .= '<form class="tag3"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				elseif( ($tag['count'] >= $min+$div*4) && ($tag['count'] < $min+$div*5) )
					$string .= '<form class="tag2"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				elseif( ($tag['count'] >= $min+$div*5) && ($tag['count'] <= $min+$div*6) )
					$string .= '<form class="tag1"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
				else
					$string .= '<form class="tag1"><a href="search.php?id='.$ID.'">'.$tag['name'].'</a></form> ';
			}
			
			return $string;
		}
		
		/*
		 * retrieves the name for the tag
		 * takes an id for the tag (should have been escaped already)
		 * returns string with name
		 */
		static function getTagNameByID($ID)
		{
			$DB = database::getDB();
			
			$sql = "select `DESCRIPTION` from tags where ID = $ID";
			//echo $sql; die();
			$result = mysqli_query($DB, $sql);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
				$name = $row['DESCRIPTION'];
				
			mysqli_free_result($result);
			mysqli_close($DB);
			
			return $name;
		}
	}
}
?>